﻿using Microsoft.Practices.Unity;
using Tinter.Common.Logging;
using Tinter.Common.NetImplementation;
using Tinter.Models.Modules.Finance;
using Tinter.Infrastructure.Data.Modules.Finance;
using Tinter.Infrastructure.Data.UnitOfWork;

namespace Tinter.Wpf.ServiceAgents.InstanceProviders
{
    /// <summary>
    /// DI container accesor
    /// </summary>
    public static class Container
    {
        #region Properties

        private static IUnityContainer container;

        /// <summary>
        /// Get the current configured container
        /// </summary>
        /// <returns>Configured container</returns>
        public static IUnityContainer Current
        {
            get
            {
                return container;
            }
        }

        #endregion

        #region Constructor

        static Container()
        {
            ConfigureContainer();
            ConfigureFactories();
        }

        #endregion

        #region Methods

        private static void ConfigureContainer()
        {
            /*
             * Add here the code configuration or the call to configure the container 
             * using the application configuration file
             */

            container = new UnityContainer();

            //-> Unit of Work and repositories
            container.RegisterType<IMainUnitOfWork, TinterEntities>(new PerResolveLifetimeManager());

            container.RegisterType<IExpenseProjectRepository, ExpenseProjectRepository>();

            /*
             * examples:
            _currentContainer.RegisterType<IBankAccountRepository, BankAccountRepository>();
            _currentContainer.RegisterType<ICountryRepository, CountryRepository>();
            _currentContainer.RegisterType<ICustomerRepository, CustomerRepository>();
            _currentContainer.RegisterType<IOrderRepository, OrderRepository>();
            _currentContainer.RegisterType<IProductRepository, ProductRepository>();

            //-> Adapters
            _currentContainer.RegisterType<ITypeAdapter, TypeAdapter>();
            _currentContainer.RegisterType<RegisterTypesMap, ERPModuleRegisterTypesMap>("erpmodule");
            _currentContainer.RegisterType<RegisterTypesMap, BankingModuleRegisterTypesMap>("bankingmodule");

            //-> Domain Services
            _currentContainer.RegisterType<IBankTransferService, BankTransferService>();

            //-> Application services
            _currentContainer.RegisterType<ISalesAppService, SalesAppService>();
            _currentContainer.RegisterType<ICustomerAppService, CustomerAppService>();
            _currentContainer.RegisterType<IBankAppService, BankAppService>();

            //-> Distributed Services
            _currentContainer.RegisterType<IBankingModuleService, BankingModuleService>();
            _currentContainer.RegisterType<IERPModuleService, ERPModuleService>();
             */
        }

        private static void ConfigureFactories()
        {
            LoggerFactory.SetCurrent(new TraceSourceLogFactory());
            /*
            EntityValidatorFactory.SetCurrent(new DataAnnotationsEntityValidatorFactory());
             */
        }

        #endregion
    }
}